﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Helpers.IoC;
using ReAnimation.Infrastructure.Services.Interfaces;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace ReAnimation.MVP.Presenters
{
    public class VideoDialogPresenter: BasePresenter<IVideoDialogView>
    {
        private IMainModel _mainModel;
        private ILazyInject<ISaveFileDialogView> _saveFileDialog;
        private IAnimationToPOVService _animationToPOVService;
        private String _outFileName;

        public VideoDialogPresenter(IMainModel mainModel,
                                    ILazyInject<ISaveFileDialogView> saveFileDialog,
            IAnimationToPOVService animationToPOVService)
        {
            _mainModel = mainModel;
            _saveFileDialog = saveFileDialog;
            _animationToPOVService = animationToPOVService;
        }

        protected override void InitView()
        {
            View.CreateVideo += new EventHandler(View_CreateVideo);
            View.ChooseFile += new EventHandler(View_ChooseFile);
        }

        void View_ChooseFile(object sender, EventArgs e)
        {
            ISaveFileDialogView saveFileDialog = _saveFileDialog.Get();
            if (saveFileDialog.ShowOpenFileDialog())
            {
                _outFileName = saveFileDialog.FileName;
                View.OutFilePath = _outFileName;
            }
        }

        void View_CreateVideo(object sender, EventArgs e)
        {
            
            _animationToPOVService.ConvertAnimationToPOV(View.Duration, 
                View.CurrentResolution,
                View.SaveFrames, 
                View.UsePov,
                _outFileName,
                (percent) => 
                {
                    View.Percent = (int)percent;
                    Application.DoEvents();
                });
        }
    }
}
